///PHILOTES Source Code.  (C)2012 PhiloLabs
#pragma once

#ifndef __OGRE_DEFLATE_H__
#define __OGRE_DEFLATE_H__

#include "OgrePrerequisites.h"
#include "OgreDataStream.h"


/// forward decls
struct z_stream_s;
typedef struct z_stream_s z_stream;

namespace Ogre
{
	
	class _OgreExport DeflateStream : public DataStream
	{
	protected:
		DataStreamPtr mCompressedStream;
		DataStreamPtr mTmpWriteStream;
		String mTempFileName;
		z_stream* mZStream;
		size_t mCurrentPos;
		
		// Cache for read data in case skipping around
		StaticCache<16 * OGRE_STREAM_TEMP_SIZE> mReadCache;
		
		// Intermediate buffer for read / write
		unsigned char *mTmp;
		
		// Whether the underlying stream is valid compressed data
		bool mIsCompressedValid;
		
		
		void init();
		void destroy();
		void compressFinal();
	public:
		
        DeflateStream(const DataStreamPtr& compressedStream);
		
		DeflateStream(const String& name, const DataStreamPtr& compressedStream);	
		
		~DeflateStream();
		
		
		bool isCompressedStreamValid() const { return mIsCompressedValid; }
		
		
		size_t read(void* buf, size_t count);
		
		
		size_t write(const void* buf, size_t count);
				
		
		void skip(long count);
		
		
	    void seek( size_t pos );
		
		
		size_t tell(void) const;
		
		
	    bool eof(void) const;
		
        
        void close(void);
		
	};
}

#endif
